package com.faya.green.service.impl;

import com.faya.green.abnormal.ServiceException;
import com.faya.green.dao.authority.OrgPostDao;
import com.faya.green.dao.authority.OrganizationDao;
import com.faya.green.dao.authority.PostDao;
import com.faya.green.dao.authority.PostUserDao;
import com.faya.green.dao.user.UserDao;
import com.faya.green.enumeration.ResultEnum;
import com.faya.green.enumeration.StatusEnum;
import com.faya.green.model.*;
import com.faya.green.service.PostService;
import com.faya.green.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户 ：LX
 * 创建时间： 2018/8/8. 8:47
 * 地点：广州
 * 目的: 岗位业务层
 * 结果：
 */
@Service
public class PostServiceImpl implements PostService {

    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private OrganizationDao organizationDao;
    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private PostDao postDao;
    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private OrgPostDao orgPostDao;
    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private PostUserDao postUserDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Post addOrUpdatePost(String operationType, Post post, String orgId) {
        if (post == null){
            throw new ServiceException("岗位信息不能为空");
        }
        if ("upd".equals(operationType)){
            if (CommonUtils.checkNull(post.getId())){
                throw new ServiceException("更新无法获取到岗位id");
            }
        }
        if (CommonUtils.checkNull(orgId)){
            throw new ServiceException("无法获取到机构信息，请刷新重试");
        }
        if (CommonUtils.checkNull(post.getName())){
            throw new ServiceException("机构名不能为空");
        }

        Organization organization = organizationDao.getOrganizationNodelById(orgId);
        if (organization == null){
            throw new ServiceException("机构信息获取为空，找不到对应的机构");
        }

        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipal();
        if ("add".equals(operationType)){
            post.setId(UUID.randomUUID().toString());
            post.setCreateUser(user.getId());
            post.setStatus(StatusEnum.STATUS_ADD.getCode());
            post.setCreateTime(new Date());
            post.setUpdateUser(user.getId());
            post.setUpdateTime(new Date());
            postDao.addPost(post);

            //关系数据添加
            OrgPost orgPost = new OrgPost();
            orgPost.setId(UUID.randomUUID().toString());
            orgPost.setOrgId(orgId);
            orgPost.setPostId(post.getId());
            orgPost.setStatus(StatusEnum.STATUS_ADD.getCode());
            orgPost.setCreateTime(new Date());
            orgPost.setUpdateTime(new Date());
            orgPostDao.addOrgPost(orgPost);

        } else {
            post.setStatus(StatusEnum.STATUS_UPDATE.getCode());
            post.setUpdateUser(user.getId());
            post.setUpdateTime(new Date());
            postDao.updatePost(post);
        }

        return post;
    }

    @Override
    public List<Post> getPostOnOrg(String orgId) {
        if (CommonUtils.checkNull(orgId)){
            throw new ServiceException("机构|部门ID为空");
        }

        List<Post> postByOrgId = postDao.getAllPostByOrgId(orgId);
        return postByOrgId;
    }

    @Override
    public ResultJson getPostOnOrgPage(String orgId, Integer page, Integer limit) {
        if (CommonUtils.checkNull(orgId)){
            throw new ServiceException("机构|部门ID为空");
        }

        if (page == null || limit == null || page < 0 || limit < 0){
            PageHelper.startPage(1, 10);
        } else {
            //这段代码表示，程序开始分页了，page默认值是1，pageSize默认是10，意思是从第1页开始，每页显示10条记录
            PageHelper.startPage(page, limit);
        }
        //分页助手会自动分页
        List<Post> posts = postDao.getAllPostByOrgId(orgId);
        PageInfo<Post> pageInfo = new PageInfo<Post>(posts);
        return new ResultJson(null, ResultEnum.RESULT_SUCCESS.getStatus(), "查询成功", pageInfo);
    }

    @Override
    public Post getPostById(String id) {
        return postDao.getPostById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delPost(String postId) {
        if (CommonUtils.checkNull(postId)){
            throw new ServiceException("岗位id不能为空");
        }

        Post post = postDao.getPostById(postId);
        if (post == null){
            throw new ServiceException("无法找到岗位，请刷新重试");
        }

        List<PostUser> postUserList = postUserDao.getPostUserByPostId(post.getId());
        if (postUserList != null && postUserList.size() > 0){
            throw new ServiceException("岗位下还存在用户，不能执行删除");
        }

        postDao.delPost(post.getId());
        OrgPost orgPost = orgPostDao.getOrgPostByPostId(post.getId());
        if (orgPost == null){
            throw new ServiceException("未找到岗位和机构映射关系，无法删除");
        }
        orgPostDao.delOrgPost(orgPost.getId());
    }

}
